﻿var pack = new Package();
packageContent = function(operateType, initPack) {
	var actionItemMap = new HashMap();
	var objID = 1;
	var objIndex = 1;
	var objIndexTemp = 1;
	var actionIndex = 1;
	var actionIndexTemp = 0;
	var item = 0;
	var itemID = 0;
	var select = "";
	// 基本信息
	var _openBaseInfoPage = function() {
		$("#openBaseInfoPage").window({
					title : '包基本信息设置',
					width : 700,
					height : 510,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});

		$("#openBaseInfoPage").window('open');
		$("#openBaseInfoPage").window('refresh', 'packageBaseInfo.jsp');

	}

	// 参与对象
	var _openPartObjPage = function(objId) {
		$("#openPartObjPage").window({
					title : '包参与对象设置',
					width : 400,
					height : 260,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});
		$("#openPartObjPage").window('open');
		$("#openPartObjPage").window('refresh',
				'packagePartObj.jsp?partObjId=' + objId);

	}
	// 动作
	var _openActionTypePage = function(actionItemId) {
		$("#openActionTypePage").window({
					title : '条目动作设置',
					width : 400,
					height : 150,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});

		$("#openActionTypePage").window('open');
		$("#openActionTypePage").window('refresh',
				'packageActionType.jsp?actionItemId=' + actionItemId);

	}
	// 动作基本信息
	var _openActionBaseInfoPage = function(actionItemId) {
		$("#openActionBaseInfoPage").window({
					title : '条目基本信息设置',
					width : 400,
					height : 230,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});

		$("#openActionBaseInfoPage").window('open');
		$("#openActionBaseInfoPage").window('refresh',
				'packageActionBaseInfo.jsp?actionItemId=' + actionItemId);

	}

	// 目标对象
	var _openEffectObjPage = function(actionItemId, effectObjID, partObjId,
			actionType, partObjCD) {
		$("#openEffectObjPage").window({
					title : '目标对象设置',
					width : 400,
					height : 230,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});

		$("#openEffectObjPage").window('open');
		$("#openEffectObjPage").window(
				'refresh',
				'pacakgeEffectObj.jsp?actionItemId=' + actionItemId
						+ "&effectObjID=" + effectObjID + "&partObjId="
						+ partObjId + "&actionType=" + actionType
						+ "&partObjCD=" + partObjCD);

	}

	// 条目参数
	var _openActionParameterPage = function(actionItemId) {
		$("#openActionParameterPage").window({
					title : '条目参数设置',
					width : 700,
					height : 310,
					modal : true,
					shadow : false,
					closed : true,
					resizable : false,
					draggable : true,
					minimizable : false,
					maximizable : false
				});

		$("#openActionParameterPage").window('open');
		$("#openActionParameterPage").window('refresh',
				'packageActionParameter.jsp?actionItemId=' + actionItemId);

	}

	// 默认加载
	var _load = function(initPack) {
		// 查看
		if (operateType == PACKAGE.OPERATE.SHOW) {

			var initPack = JSON.parse(initPack);
			createTable(initPack, PACKAGE.OPERATE.SHOW);

		}
	}

	var createTable = function(initPack, ot) {
		//参与
		var packObjMap = new HashMap();

		var pObj = initPack.packageObjList;
		for (var index = 0; index < pObj.length; index++) {
			var partObj = convertPackageObj(pObj[index]);
			packObjMap.put(partObj.objectNumber, partObj);
		}
		pack.setPackObjMap(packObjMap);



		// total table ru kou
		var packTable = $("<table></table>");
		packTable.attr("style","border:solid 1px #add9c0;")
		packTable.attr("id", initPack.packageId);
		var packTr = $("<tr></tr>");
		var baseTD = $("<td></td>");
		// 创建包基本信息
		var baeeInfo = convertBaseinfo(initPack);
		pack.setPackageBaseInfo(baeeInfo);
		var spanBase = $("<div></div>").append(initPack.name);
		spanBase.attr("id", "p_config");
		spanBase.bind("click", function() {
					_openBaseInfoPage();
				});
		baseTD.append(spanBase);
		if (ot != PACKAGE.OPERATE.SHOW) {
			var imgAdd = $("<img>");
			imgAdd.addClass("imgAdd");
			imgAdd.attr("align", "right");
			imgAdd.bind("click", function() {
						_addObj();
					});
			baseTD.append(imgAdd);
		}
		packTr.append(baseTD);
		// 包参与对象
		var partTD = $("<td></td>");

		packTr.append(partTD);
		var partTable = $("<table ></table>")
		partTable.attr("style","border:solid 1px #add9c0;");


		for (var index = 0; index < pObj.length; index++) {
			// 类型为1 客户
			if (pObj[index].objType == 1) {

				var partTr = createPartObjTable(pObj[index],
						initPack.actionOrderItemList);

				partTable.append(partTr);
			}
			// 类型为2 产品
			if (pObj[index].objType == 2) {
				var partTr = createPartObjTable(pObj[index],
						initPack.actionOrderItemList);
				partTable.append(partTr);
			}

		}

		pack.setActionItemMap(actionItemMap);
		partTD.append(partTable);
		packTable.append(packTr)

		var preview = $("#previewDiv");

		preview.append(packTable);

	}

	createPartObjTable = function(pObj, pItem) {


		var partTr = $("<tr>  </tr>");
		var partTd1 = $("<td>-----</td>");
		var partTd2 = $("<td></td>");

		var partdiv = $("<div></div>")
		partdiv.attr("id", pObj.objectNumber);
		partdiv.append(pObj.objectName);
		partdiv.bind("click", function() {
					_openPartObjPage($(this).attr("id"));
				});
		partTd2.append(partdiv);

		var partTd3 = $("<td></td>");
		var itemTable = $("<table></table>");
		itemTable.attr("style","border:solid 1px #add9c0;")
		//itemTable.attr("border", "0");
		// 参与对象编码
		var pnbr = pObj.objectNumber;
		// 处理动作条目
		for (var i = 0; i < pItem.length; i++) {
			var nbr = pItem[i].partobjNumber
			if (nbr == pnbr) {
				// todo
				var itr = createItemTable(pItem[i]);
				itemTable.append(itr);
			}

		}
		partTd3.append(itemTable);
		partTr.append(partTd1);
		partTr.append(partTd2);
		partTr.append(partTd3);
		return partTr;
	}
	// 创建条目表格
	var createItemTable = function(pItem) {

		var item = convertPackageActionItem(pItem);
		actionItemMap.put(pItem.actionItemId, item);

		var itemTr = $("<tr></tr>");
		var itemTd1 = $("<td>----</td>");
		var itemTd2 = $("<td></td>");
		var iTe = $("<table  > </table>");

		iTe.attr("style","border:solid 1px #add9c0;")
		var iTr1 = $("<tr></tr>");
		var iTDdz = $(" <td></td>");
		var typeDiv = $("<div></div>");
		typeDiv.attr("id", pItem.actionItemId);

		typeDiv.append(pItem.actionTypeName);
	//	typeDiv.append(pItem.actionTypeCd);
		typeDiv.bind("click", function() {
					var itemId = $(this).attr("id");
					_openActionTypePage(itemId);
				});
		iTDdz.append(typeDiv);
		var iTDmb = $("<td></td>");

		var spanmb = $("<div></div>")
		spanmb.attr("id", pItem.actionItemId);
		spanmb.attr("partobj", pItem.partobjNumber);
		spanmb.attr("effectObj", pItem.effectobjNumber);
		spanmb.attr("actionTypeCd", pItem.actionTypeCd);

		// chu shi hua can yu duixiang d zhi

		var partObj = pack.getPackObjMap().get(pItem.partobjNumber);
		var effectObj = pack.getPackObjMap().get(pItem.effectobjNumber);

		spanmb.attr("partObjId", partObj.getObjId());
		spanmb.append(effectObj.getObjectName());
		spanmb.bind("click", function() {
					var itemId = $(this).attr("id");
					var effectObj = $(this).attr("effectObj");
					var partObjId = $(this).attr("partObjId");
					var partObjNbr = $(this).attr("partobj");
					var actionType = $(this).attr("actionTypeCd");
					_openEffectObjPage(itemId, effectObj, partObjId,
							actionType, partObjNbr);
				});

		iTDmb.append(spanmb);
		var iTDcs = $(" <td></td>");
		var csDiv = $("<div></div>");
		csDiv.attr("id", pItem.actionItemId);

		csDiv.click();
		csDiv.append("<a>参数</a>");
		csDiv.bind("click", function() {
					var itemId = $(this).attr("id");
					_openActionParameterPage(itemId);
				});
		iTDcs.append(csDiv);
		iTr1.append(iTDdz);
		iTr1.append(iTDmb);
		iTr1.append(iTDcs);
		iTe.append(iTr1);
		var iTr2 = $("<tr></tr>");
		var iTDdesc = $(" <td  colspan='3'></td>");

		var descDiv = $("<div></div>");
		descDiv.attr("id", pItem.actionItemId);
		descDiv.append(pItem.description);
		descDiv.bind("click", function() {
					var itemId = $(this).attr("id");
					_openActionBaseInfoPage(itemId);
				});
		iTDdesc.append(descDiv);
		var iTDmb = $(" <td> " + pItem.partobjNumber + "</td>");

		iTr2.append(iTDdesc);
		iTe.append(iTr2);
		itemTd2.append(iTe);
		itemTr.append(itemTd1);
		itemTr.append(itemTd2);
		return itemTr;
	}

	_load(initPack);
}

// 创建参与对象表格

function convertBaseinfo(obj) {
	var baseInfo = new PackageBaseInfo();
	baseInfo.setPackageId(obj.packageId);
	baseInfo.setPackageCode(obj.packageCode);
	baseInfo.setReceiptContent(obj.receiptContent);
	baseInfo.setPackagePicUrl(obj.packagePicUrl);
	baseInfo.setPackageAD(obj.packageAD);
	baseInfo.setAreaId(obj.areaId);
	baseInfo.setAreaName(obj.areaName);
	baseInfo.setStartDate(obj.startDate);
	baseInfo.setEndDate(obj.endDate);
	baseInfo.setStatusCd(obj.statusCd);
	// baseInfo.setStatusName(obj.statusName);
	baseInfo.setCreateDate(obj.createDate);
	baseInfo.setName(obj.name);
	baseInfo.setDescription(obj.description);
	baseInfo.setVersion(obj.version);
	return baseInfo;
}

function convertPackageObj(obj) {
	var pObj = new PackageObject();
	pObj.setPackageId(obj.packageId);
	pObj.setObjectNumber(obj.objectNumber);
	pObj.setObjectName(obj.objectName);
	pObj.setObjType(obj.objType);
	pObj.setObjId(obj.objId);
	pObj.setIsNew(obj.isNew);
	pObj.setCreateDate(obj.createDate);
	pObj.setVersion(obj.version);
	return pObj;
}
function convertPackageActionItem(obj) {
	var item = new PackageActionItem();
	item.setPackageId(obj.packageId);
	item.setCreateDate(obj.createDate);
	// item.setActionItemParameterList(obj.actionItemParameterList);
	item.setActionItemId(obj.actionItemId);
	item.setPartobjNumber(obj.partobjNumber);
	item.setActionTypeCd(obj.actionTypeCd);
	item.setActionTypeName(obj.actionTypeName);
	item.setEffectobjNumber(obj.effectobjNumber);
	item.setRoleCd(obj.roleCd);
	item.setIsVisible(obj.isVisible);
	item.setIsMust(obj.isMust);
	item.setSeqNumber(obj.seqNumber);
	item.setDescription(obj.description);
	item.setVersion(obj.version);
	return item;
}
